package com.jie.eclipse.pan.popup.wizardpage;


import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.config.BasePageConfig;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.editors.utils.PanImageHelper;
import com.jie.eclipse.pan.listener.PSelectionListener;
import com.jie.eclipse.pan.popup.wizard.BaseWizard;
import com.jie.eclipse.pan.popup.wizard.CreatePageTemplateWizard;
import com.jie.eclipse.pan.popup.wizardpage.grid.Grid2CrudLeftPage;
import com.jie.eclipse.pan.popup.wizardpage.grid.Grid1CrudPage;
import com.jie.eclipse.pan.swtitem.ListGroup;

public class FirstPage extends BasePage{
	Combo tpType = null;//模板类型
	Text pageName = null;//页面名称
	Text pageTitle = null;//页面标题
	BaseDataBind toolbarBind = null ;//工具栏绑定
	Map<String, Image> imgMap = null;
	
	public FirstPage() {
		super(CreatePageTemplateWizard.page_1,"",PanImageHelper.getInstance().getPanImageDescriptor());
		//设置标题消息
		this.setMessage("基本配置：");
	}
	
	public Image getTpImage(String tpType){
		if(imgMap == null){
			this.imgMap = new HashMap<String, Image>();
		}
		
		if(this.imgMap.containsKey(tpType)){
			return this.imgMap.get(tpType);
		}else{
			Image image = PanImageHelper.getInstance().getImage("icons/tpimg/"+tpType+".jpg");
			this.imgMap.put(tpType, image);
			return image;
		}
		
	}
	
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.BORDER);
		composite.setLayout(new GridLayout(1, true));
		
		Group group1 = new Group(composite, SWT.NULL);
		group1.setText("模板属性"); 
		group1.setToolTipText("模板属性");
		group1.setLayout(new GridLayout(2, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label label1= new Label(group1, SWT.RIGHT);
		label1.setText("选择模板:");
		
		tpType = new Combo(group1, SWT.READ_ONLY);	
		tpType.setItems(new String[]{"基本_表格模板","基本_表单模板","基本_计件(Loading)模板","基本_列印模板","基本_报表模板","高级_表格","高级_左查右编"});
		
		tpType.setData("0", BasePageConfig.TAG_TPTYPE_BASE_GRID);
		tpType.setData("1", BasePageConfig.TAG_TPTYPE_BASE_FORM);
		tpType.setData("2", BasePageConfig.TAG_TPTYPE_BASE_LOADING);
		tpType.setData("3", BasePageConfig.TAG_TPTYPE_BASE_PRINT);
		tpType.setData("4", BasePageConfig.TAG_TPTYPE_BASE_REPORT);
		tpType.setData("5", BasePageConfig.TAG_TPTYPE_ADVANCED_GRID1);
		tpType.setData("6", BasePageConfig.TAG_TPTYPE_ADVANCED_GRID2);
		
		tpType.select(0);
		tpType.setVisible(true);
		this.getDataBind().bind("tpType",tpType);
		
		
		Group group2 = new Group(composite, SWT.NULL);
		group2.setText("页面属性");
		group2.setToolTipText("页面属性");
		group2.setLayout(new GridLayout(2, false));
		group2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label label2= new Label(group2, SWT.RIGHT);
		label2.setText("页面名称:");
		pageName = new Text(group2, SWT.BORDER);
		pageName.setLayoutData(new GridData(300, 15));
		pageName.setText(this.getBaseWizard().getCurrFolderName().replace("jm", "").toLowerCase());//设置页面名称默认的前缀
		this.getDataBind().bind("pageName",pageName);
		
		Label label3= new Label(group2, SWT.RIGHT);
		label3.setText("页面标题:");
		pageTitle = new Text(group2, SWT.BORDER);
		pageTitle.setLayoutData(new GridData(300, 15));
		pageTitle.setText("新建页面");
		this.getDataBind().bind("pageTitle",pageTitle);
		
		//配置工具栏
		Group group3 = new Group(composite, SWT.NULL);
		group3.setText("模板图片预览");
		group3.setLayout(new StackLayout());
		group3.setLayoutData(new GridData(GridData.FILL_BOTH));
		
//		Composite cop1 = new Composite(group3, SWT.NONE);
//		cop1.setLayout(new GridLayout(6, true));
//		
//		toolbarBind = this.getNewDataBind();
//		newToolbarButtonConfig("new","新增",cop1,toolbarBind);
//		newToolbarButtonConfig("search","查询",cop1,toolbarBind);
//		newToolbarButtonConfig("save","储存",cop1,toolbarBind);
//		newToolbarButtonConfig("cancel","取消",cop1,toolbarBind);
//		newToolbarButtonConfig("delete","删除",cop1,toolbarBind);
//		newToolbarButtonConfig("copy","复制",cop1,toolbarBind);
//		newToolbarButtonConfig("start","送签",cop1,toolbarBind);
//		newToolbarButtonConfig("check","审核",cop1,toolbarBind);
//		newToolbarButtonConfig("export","导出",cop1,toolbarBind);
//		newToolbarButtonConfig("print","列印",cop1,toolbarBind);
//		newToolbarButtonConfig("help","帮助",cop1,toolbarBind);
		//初始化工具栏的值
//		BaseDataEntity defaultData = getBaseWizard().getPanGenerator().getDefaultToolbar(BasePageConfig.TAG_TPTYPE_BASE_GRID);
//		toolbarBind.setRecord(defaultData);
		//绑定工具栏
//		this.getDataBind().bind("tbConfig", toolbarBind);
		
//		((StackLayout)group3.getLayout()).topControl = cop1;
		
		Composite cop2 = new Composite(group3, SWT.NONE);
		cop2.setLayout(new FillLayout());
		cop2.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		Label imgLabel= new Label(cop2, SWT.RIGHT);
		imgLabel.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.drawImage(getTpImage(getDataBind().getValue("tpType")), 0,0);
			}
		});
		
		((StackLayout)group3.getLayout()).topControl = cop2;
		
		BaseDataEntity pdata =  new BaseDataEntity();
		pdata.spov("group3", group3);
//		pdata.spov("cop1", cop1);
		pdata.spov("cop2", cop2);
		pdata.spov("imgLabel", imgLabel);
		pdata.spov("page", this);
		
		//模板控件绑定事件
		tpType.addSelectionListener(new PSelectionListener(pdata) {
			public void widgetSelected(SelectionEvent paramSelectionEvent) {
				Group group3 = (Group)this.getData().gpov("group3");
//				Composite cop1 = (Composite)this.getData().gpov("cop1");
				Composite cop2 = (Composite)this.getData().gpov("cop2");
				Label imgLabel = (Label)this.getData().gpov("imgLabel");
				
//				BasePage page = (BasePage)this.getData().gpov("page");
				
//				String value = String.valueOf(tpType.getData(String.valueOf(tpType.getSelectionIndex())));
//				toolbarBind.setRecord(getBaseWizard().getPanGenerator().getDefaultToolbar(value));
				
				StackLayout sl = (StackLayout)group3.getLayout();
//				if(value.toLowerCase().startsWith("advanced")){
//					imgLabel.redraw();
//					sl.topControl = cop2;
//					group3.setText("模板图片预览");
//				}else{
//					group3.setText("工具栏配置");
//					sl.topControl = cop1;
//					
//				}
				imgLabel.redraw();
				sl.topControl = cop2;
				
				group3.layout();
			}
		});
		
		
		
		// 该方法非常重要，否则不能显示设置的控件
		setControl(composite);
	}
	
	protected Group newToolbarButtonConfig(String name,String text,Composite composite,BaseDataBind dataBind){
		Group group = new Group(composite, SWT.NULL);
		group.setLayout(new GridLayout(2, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		GridData colspsn1 = new GridData(GridData.FILL_HORIZONTAL);
		
		GridData colspsn2 = new GridData(GridData.FILL_HORIZONTAL);
		colspsn2.horizontalSpan = 2;
		
		Label label1 = new Label(group, SWT.LEFT);
		label1.setFont(new Font(null, "宋体",  10,   SWT.BOLD));
		label1.setLayoutData(colspsn2);
		label1.setText(text);
		
		new Label(group, SWT.RIGHT).setText("启用:");
//		Combo combo1 = new Combo(group, SWT.READ_ONLY);
//		combo1.setItems(new String[]{"是","否"});
//		combo1.setData("0", true);
//		combo1.setData("1", false);
//		dataBind.bind(name+"Disabled", combo1);
		
		ListGroup group1 = new ListGroup(group, "radio");
		group1.setLayout(new GridLayout(2, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		group1.setData("xtype", "ridio");
		Button btn1 = group1.getNewButton();
		btn1.setData(true);
		btn1.setText("是");
		Button btn11= group1.getNewButton();
		btn11.setData(false);
		btn11.setText("否");
		dataBind.bind(name+"Disabled", group1);
		
		new Label(group, SWT.RIGHT).setText("提示:");
//		Combo combo2 = new Combo(group, SWT.READ_ONLY);
//		combo2.setItems(new String[]{"是","否"});
//		combo2.setData("0", true);
//		combo2.setData("1", false);
//		dataBind.bind(name+"Confirm", combo2);
		ListGroup group2 = new ListGroup(group, "radio");
		group2.setLayout(new GridLayout(2, false));
		group2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		group2.setData("xtype", "ridio");
		Button btn2 = group2.getNewButton();
		btn2.setData(true);
		btn2.setText("是");
		Button btn22 = group2.getNewButton();
		btn22.setData(false);
		btn22.setText("否");
		dataBind.bind(name+"Confirm", group2);
		
		new Label(group, SWT.RIGHT).setText("显示:");
//		Combo combo3 = new Combo(group, SWT.READ_ONLY);
//		combo3.setItems(new String[]{"是","否"});
//		combo3.setData("0", true);
//		combo3.setData("1", false);
//		dataBind.bind(name+"Enab", combo3);
		
		ListGroup group3 = new ListGroup(group, "radio");
		group3.setLayout(new GridLayout(2, false));
		group3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		group3.setData("xtype", "ridio");
		Button btn3 = group3.getNewButton();
		btn3.setData(true);
		btn3.setText("是");
		Button btn33= group3.getNewButton();
		btn33.setData(false);
		btn33.setText("否");
		dataBind.bind(name+"Enab", group3);
		
//		new Label(group, SWT.RIGHT).setText("菜单宽度(px)");
//		new Text(group, SWT.BORDER).setLayoutData(colspsn1);
//		
//		new Label(group, SWT.RIGHT).setText("菜单项");
//		new Text(group, SWT.BORDER).setLayoutData(colspsn1);
		
		
		return group;
	}
	
	/* (non-Javadoc)
	 * @see com.jie.eclipse.pan.popup.wizardpage.BasePage#getNxtPage()
	 */
	@Override
	public BasePage getNxtPage() {
		String strTpType = this.getDataBind().getRecord().gpsv("tpType");
		if(secPageMap == null){
			secPageMap = new HashMap<String, BasePage>();
		}
		
		if(secPageMap.containsKey(strTpType)){
			return secPageMap.get(strTpType);
		}else if(BasePageConfig.TAG_TPTYPE_ADVANCED_GRID1.equalsIgnoreCase(strTpType)){
			Grid1CrudPage gridPage = new Grid1CrudPage();
			gridPage.setBaseWizard((BaseWizard)getWizard());
			((BaseWizard)getWizard()).addPage(gridPage);
			secPageMap.put(strTpType, gridPage);
			return gridPage;
		}else if(BasePageConfig.TAG_TPTYPE_ADVANCED_GRID2.equalsIgnoreCase(strTpType)){
			Grid2CrudLeftPage leftPage = new Grid2CrudLeftPage();
			leftPage.setBaseWizard((BaseWizard)getWizard());
			((BaseWizard)getWizard()).addPage(leftPage);
			secPageMap.put(strTpType, leftPage);
			return leftPage;
		}
		return null;
	}
}
